package algotithm.acwing.math.test87;

/**
 * @author 25068
 * @Date 2021/6/13 10:36
 * @Title acwing87 把字符串转换成整数
 */
public class Main {

    public static void main(String[] args) {
        String s = "-0000002147483648";
        System.out.println(strToInt(s));
    }

    public static int strToInt(String s) {
        int k = 0;
        char[] c = s.toCharArray();
        int n = c.length;
        int minus = 1;
        while (k < n && c[k] == ' ') {
            k++;
        }
        if (k < n && c[k] == '+') {
            k++;
        } else if (k < n && c[k] == '-') {
            minus = -1;
            k++;
        }
        long res = 0;
        while (k < n && c[k] >= '0' && c[k] <= '9') {
            res = res * 10 + c[k] - '0';
            k++;
        }
        res = res * minus;
        if (res > Integer.MAX_VALUE) {
            return Integer.MAX_VALUE;
        } else if (res < Integer.MIN_VALUE) {
            return Integer.MIN_VALUE;
        }
        return (int)res * minus;
    }

}
